package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckitemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* @Author : wangjie
* @CreateDate : 2020/3/27 15:50
* @Version : 1.0
* @Description : 检查项前端控制器
*/
@RestController
@RequestMapping("checkitem")
@Slf4j
public class CheckitemController {
    //依赖注入
    @Reference
    private CheckitemService checkitemService;

    /**
     * 检查项分页
     * @param queryPageBean(三个参数:当前页,每页显示条数,显示数据)
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")//权限控制
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            //调用service 获取分页结果
            PageResult pageResult = checkitemService.pageQuery(queryPageBean);
            //成功,返回结果
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            //失败,返回初始化结果
            return new PageResult(0l,null);
        }
    }

    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @RequestMapping("add")
    public Result add(@RequestBody CheckItem checkItem){
        try {
            //调用service 实现新增
            checkitemService.add(checkItem);
            return new Result(true, MessageConst.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ADD_CHECKITEM_FAIL);
        }
    }

    /**
     * 基于ID 删除检查项
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("delete")
    public Result deleteById(Integer id){
        try {
            //调用service 方法
            checkitemService.deleteById(id);
            //删除成功,返回消息
            return new Result(true,MessageConst.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败,返回消息
            return new Result(false,MessageConst.DELETE_CHECKITEM_FAIL);
        }
    }

    /**
     * 数据回调
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            //调用service 方法
            CheckItem checkItem = checkitemService.findById(id);
            //返回成功数据
            return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败信息
            return new Result(false,MessageConst.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 修改检查项
     * @param checkItem
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @RequestMapping("edit")
    public Result edit(@RequestBody CheckItem checkItem){
        log.debug("checkitem:{}",checkItem);
        try {
            //调用service 方法
            checkitemService.edit(checkItem);
            //返回成功消息
            return new Result(true,MessageConst.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败消息
            return new Result(false,MessageConst.EDIT_CHECKITEM_FAIL);
        }
    }

    /**
     * 查询检查项列表
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
        @RequestMapping("findAll")
        public Result findAll(){
            try {
                //调用service 方法
                List<CheckItem> checkItems= checkitemService.findAll();
                //返回查询成功结果
                return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItems);
            } catch (Exception e) {
                e.printStackTrace();
                //返回查询失败结果
                return new Result(false,MessageConst.QUERY_CHECKITEM_FAIL);
            }
    }
}
